from django.contrib.auth.models import User
from django.db import models

# Create your models here.
from utils.modelsMixin import ModelSetMixin, DateTimeModelMixin
from user.models import Address


# 类目表
class Classification(ModelSetMixin):  # 继承 创建时间 修改时间 逻辑删除 整合类
    name = models.CharField('分类名', max_length=40)
    # 一对多关联表本身,字段删除上级或下级不受影响
    parent = models.ForeignKey('self', verbose_name='上级分类', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'classification'
        verbose_name = '类目表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 商品表
class Commodity(ModelSetMixin):
    STATUS_CHOICE = (
        (0, '未发布'),
        (1, '发布')
    )
    name = models.CharField(verbose_name='商品名', max_length=40)
    caption = models.CharField(verbose_name='副标题', max_length=140)
    brand = models.CharField(verbose_name='品牌', max_length=40, null=True, blank=True)
    # max_digits=10——价格总位数;decimal_places=2——小数点位数
    price = models.DecimalField(verbose_name='单价', max_digits=10, decimal_places=2)
    stock = models.IntegerField(verbose_name='库存')
    pack = models.CharField(verbose_name='包装信息', max_length=140, null=True, blank=True)
    service_after_sale = models.TextField(verbose_name='售后服务', null=True, blank=True)

    sales = models.IntegerField(verbose_name='销量', default=0)
    comment = models.IntegerField(verbose_name='评论数', default=0)
    status = models.IntegerField(verbose_name='状态', choices=STATUS_CHOICE, default=0)
    detail = models.TextField(verbose_name='详情', null=True, blank=True)

    # 商品和类目的关联表字段 一对多
    # 当有多个外键关联时,为了更加方便查询处理,需要设置反向查询并更改反向查询的查询集名
    classification01 = models.ForeignKey(Classification, verbose_name='一级类目', on_delete=models.PROTECT,
                                         related_name='classification01')
    classification02 = models.ForeignKey(Classification, verbose_name='二级类目', on_delete=models.PROTECT,
                                         related_name='classification02', null=True, blank=True)

    class Meta:
        db_table = 'commodity'
        ordering = ['-sales', '-comment', '-c_time']
        verbose_name = '商品表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 商品图片表
class CommodityImg(models.Model):
    src = models.TextField(verbose_name='图片路径')
    priority = models.IntegerField(verbose_name='优先级', default=0)

    # 商品图和商品的关联表字段 一对多
    commodity = models.ForeignKey(Commodity, verbose_name='商品', on_delete=models.CASCADE)

    class Meta:
        db_table = 'commodity_img'
        ordering = ['priority']
        verbose_name = '商品图片'
        verbose_name_plural = verbose_name


# 购物车
class ShoppingCart(DateTimeModelMixin):
    number = models.IntegerField(verbose_name='数量', default=1)
    commodity = models.ForeignKey(Commodity, on_delete=models.CASCADE, verbose_name='商品')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')

    class Meta:
        ordering = ['-c_time']
        db_table = 'shopping_cart'
        verbose_name = '购物车'
        verbose_name_plural = verbose_name


# 订单表
class Order(DateTimeModelMixin):
    STATUS_CHOICE = (
        (1, '待付款'),
        (2, '待发货'),
        (3, '待收货'),
        (4, '待评价'),
        (5, '已完成'),
        (6, '已取消'),
    )
    PAY_METHOD_CHOICE = (
        (1, '支付宝'),
        (2, '货到付款'),
    )
    order_id = models.CharField(verbose_name='订单号', max_length=64, primary_key=True)
    total_count = models.IntegerField(verbose_name='商品总数', default=1)
    total_amount = models.DecimalField(verbose_name='总金额', max_digits=10, decimal_places=2)
    status = models.IntegerField(verbose_name='订单状态', default=1 , choices=STATUS_CHOICE)
    pay_method = models.IntegerField(verbose_name='支付方式', default=1, choices=PAY_METHOD_CHOICE)

    address = models.ForeignKey(Address, on_delete=models.PROTECT, verbose_name='收货地址')
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='用户')

    class Meta:
        ordering = ['-c_time']
        db_table = 'order'
        verbose_name = '订单表'
        verbose_name_plural = verbose_name


# 订单的商品
class OrderGoods(DateTimeModelMixin):
    SCORE_CHOICE = (
        (1, '一星'),
        (2, '二星'),
        (3, '三星'),
        (4, '四星'),
        (5, '五星'),
    )

    number = models.IntegerField(default=1, verbose_name='数量')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')
    comment = models.TextField(verbose_name='评价', null=True, blank=True)
    score = models.IntegerField(verbose_name='评分', choices=SCORE_CHOICE, null=True, blank=True)
    is_anonymous = models.BooleanField(default=False, verbose_name='匿名评价')

    order = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name='订单')
    commodity = models.ForeignKey(Commodity, on_delete=models.PROTECT, verbose_name='商品')

    class Meta:
        db_table = 'order_goods'
        verbose_name = '订单商品'
        verbose_name_plural = verbose_name


# 支付信息
class Payment(DateTimeModelMixin):
    trade_id = models.CharField('交易信息', max_length=100, unique=True)
    order = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name='订单')

    class Meta:
        db_table = 'payment'
        ordering = ['-c_time']
        verbose_name = '订单信息'
        verbose_name_plural = verbose_name
